Qt.include("Storage.js")

WorkerScript.onMessage = function(message) {
    doSessionConnect(message)
}

function doSessionConnect(message) {
    var xhr = new XMLHttpRequest();
    var url = "http://coscup.org/2011/api/program/"

    message.model.clear();
    var index = message.type;
    WorkerScript.sendMessage({"result": "PROGRESS"});

    xhr.open("GET", url);
    xhr.onreadystatechange = function() {
        try {
            if (xhr.readyState == XMLHttpRequest.DONE) {
                var response = JSON.parse(xhr.responseText);
                for (var i in response) {
                    var jsonData = response[i];

                    //if (index == 12)
                      //  continue;

               //     if (jsonData.type == index) {
                    var m_abstract = checkObjectNull(jsonData.abstract);
                    var m_bio = checkObjectNull(jsonData.bio);
                    var m_lang = checkObjectNull(jsonData.lang);
                    var m_speaker = checkObjectNull(jsonData.speaker);
                    var m_speakerTitle = checkObjectNull(jsonData.speakerTitle);

                        message.model.append({
                                             abstract: m_abstract,
                                             bio: m_bio,
                                             from: jsonData.from,
                                             id: jsonData.id,
                                             isBreak: jsonData.isBreak,
                                             lang: m_lang,
                                             name: jsonData.name,
                                             room: jsonData.room,
                                             speaker: m_speaker,
                                             speakerTitle: m_speakerTitle,
                                             to: jsonData.to,
                                             type: jsonData.type
                        })
                 //   }


                }
            }
            message.model.sync();
            WorkerScript.sendMessage({"result": "SUCCESS"});
        } catch (err) {
            WorkerScript.sendMessage({"result": "FAILURE"});
            //ERROR
        }
    }
    xhr.send();
}

function checkObjectNull(object) {
    if (typeof(object) == "undefined")
        object = "";

    //console.log("ttt")

    return object;
}
